package com.zhang.framelib.util.page;

/**
 * Title:BasePage 
 * Description:基本分页类（抽象）
 * @Edit_Description:
 * @version:shebao-framelib 1.0
 */
public abstract class BasePage implements java.io.Serializable, IPage {

    private static final long serialVersionUID = 1L;

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /** 每页数量的默认值. **/
    public static final int DEF_COUNT = 20;

    /** 每页最大数量.*/
    public static final int MAX_COUNT = 100;

    /** 每页的数量 .*/
    private int pageSize = DEF_COUNT;
    /** 当前页第一条位置 .*/
    private int currentResult;
    
    /**
     * @Edit_Description 分页构造函数.
     * @param pageNo 当前页.
     * @param pageSize 一页几条记录.
     * @param totalCountt 一共多少条记录.
     */
    public BasePage(int pageNo, int pageSize, int totalCountt) {
        super();
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCountt;
    }

    /** 总页数. */
    private int totalPage;

    /** 当前页 .*/
    private int pageNo = 1;

    /** 总记录数. */
    private int totalCount;

    
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * @param totalCount
     *            要设置的 totalCount.
     */
    public void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            this.totalCount = 0;
        } else {
            this.totalCount = totalCount;
        }
        int totalPage2 = getTotalPage();
        if (pageNo > totalPage2) {
            pageNo = totalPage2;
        }
    }

    /** The default constructor. */
    public BasePage() {

    }

    /**
     * 第一条数据位置.
     * 
     */
    public int getFirstResult() {
        return (pageNo - 1) * pageSize;
    }

    /**
     * @param pageSize
     *            The pageSize to set.
     */
    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = DEF_COUNT;
        } else if (pageSize > MAX_COUNT) {
            this.pageSize = MAX_COUNT;
        } else {
            this.pageSize = pageSize;
        }
    }

    /**
     * @return Returns the totalPage.
     * 
     */
    public int getTotalPage() {
        if (this.totalPage <= 0) {
            this.totalPage = totalCount / pageSize;
            if (totalPage == 0 || totalCount % pageSize != 0) {
                this.totalPage++;
            }
        }
        return this.totalPage;
    }

    @Override
    public int getPageSize() {
        return this.pageSize;
    }

    @Override
    public int getPageNo() {
        return this.pageNo;
    }

    /**
     * 是否有上一页.
     */
    @Override
    public boolean isFirstPage() {
        return pageNo <= 1;
    }

    /**
     * 是否有下一页.
     */
    @Override
    public boolean isLastPage() {
        return pageNo >= getTotalPage();
    }

    @Override
    public int getNextPage() {
        if (isLastPage()) {
            return pageNo;
        } else {
            return pageNo + 1;
        }
    }

    /**
     * 当前页是第一条记录的位置.
     */
    public int getCurrentResult() {
        currentResult = (getPageNo() - 1) * getPageSize();
        if (currentResult < 0) {
            currentResult = 0;
        }
        return currentResult;
    }

    @Override
    public int getPrePage() {
        if (isFirstPage()) {
            return pageNo;
        } else {
            return pageNo - 1;
        }
    }
}
